package mmoon.mmoonstory;

import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.ai.attributes.IAttribute;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntitySnowball;
import net.minecraft.init.SoundEvents;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.GameType;
import org.jetbrains.annotations.NotNull;

import java.util.Random;

public final class EasyCommand {

    public static void lotsOfDamage(EntityPlayer player, String type, float damage) {
        player.attackEntityFrom(
                new DamageSource(type)
                        .setDamageAllowedInCreativeMode()
                        .setDamageBypassesArmor()
                        .setDamageIsAbsolute(),
                damage);
    }

    public static void lotsOfDamage(EntityPlayer player, String type) {
        lotsOfDamage(player, type, Float.MAX_VALUE);
    }

    public static void Kill(EntityPlayer player) {
        lotsOfDamage(player, "starve");
        player.setDead();
        player.isDead = true;
    }

    public static void gameMode(EntityPlayer player, int mode) {
        switch (mode) {
            case 0:
                player.setGameType(GameType.SURVIVAL);
                return;
            case 1:
                player.setGameType(GameType.CREATIVE);
                return;
            case 2:
                player.setGameType(GameType.ADVENTURE);
                return;
            case 3:
                player.setGameType(GameType.SPECTATOR);
                return;
            default:
                player.setGameType(GameType.NOT_SET);
        }
    }

    public static void XP(EntityPlayer player, int amount) {
        player.addExperience(amount);
    }

    public static void XPL(EntityPlayer player, int levels) {
        player.addExperienceLevel(levels);
    }

    public static void msg(EntityPlayer player, String message) {
        player.sendMessage(new TextComponentString(message));
    }

    public static void makeSnowBall(EntityPlayer player) {
        /* 从player处扔出一个雪球 */
        player.world.playSound(
                null,
                player.posX,
                player.posY,
                player.posZ,
                SoundEvents.ENTITY_SNOWBALL_THROW,
                SoundCategory.NEUTRAL,
                0.5F,
                0.4F / (new Random().nextFloat() * 0.4F + 0.8F));
        if (!player.world.isRemote) {
            EntitySnowball snowball = new EntitySnowball(player.world, player);
            snowball.shoot(player, player.rotationPitch, player.rotationYaw, 0.0F, 4F, 1F);
            player.world.spawnEntity(snowball);
        }
    }

    public static double getAttr(@NotNull EntityLivingBase living, IAttribute attr) {
        return living.getAttributeMap().getAttributeInstance(attr).getAttributeValue();
    }
}
